package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin/product")

public class SpuManageController {

    @Autowired
    private ManageService manageService;

    @GetMapping("{page}/{limit}")
    public Result getSpuInfoList(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SpuInfo spuInfo) {
        Page pageModel = new Page(page, limit);

        IPage<SpuInfo> iPage = this.manageService.getSpuInfoList(pageModel, spuInfo);

        return Result.ok(iPage);
    }

    //  /admin/product/baseSaleAttrList
    //  获取所有的销售属性数据：
    @GetMapping("baseSaleAttrList")
    public Result getSaleAttrList() {
        //  调用服务层方法：
        List<BaseSaleAttr> baseSaleAttrList = this.manageService.getSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //    /admin/product/saveSpuInfo
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {

        //调用保存方法
        this.manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //  /admin/product/spuImageList/{spuId}
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId) {
        //调用服务层方法
        List<SpuImage> spuImageList = this.manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //  /admin/product/spuSaleAttrList/{spuId}
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId) {
        //返回的泛型
        List<SpuSaleAttr> spuSaleAttrList = this.manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    @GetMapping("getSpuInfo/{spuId}")
    public Result getSpuInfo(@PathVariable Long spuId) {
        return Result.ok(manageService.getFullSpuInfo(spuId));
    }

    @PostMapping("updateSpuInfo")
    public Result updateSpuInfo(@RequestBody SpuInfo spuInfo) {
        // 调用服务层的保存方法
        manageService.updateSpuInfo(spuInfo);
        return Result.ok();
    }


}
